package com.tigase.client.demo;

import junit.framework.Assert;
import junit.framework.TestCase;
import tigase.jaxmpp.core.client.JID;
import tigase.jaxmpp.core.client.exceptions.JaxmppException;
import tigase.jaxmpp.core.client.xmpp.stanzas.Message;
import tigase.jaxmpp.j2se.ConnectionConfiguration;
import tigase.jaxmpp.j2se.ConnectionConfiguration.ConnectionType;
import tigase.jaxmpp.j2se.Jaxmpp;

/**
 * tigase client
 * 
 */
public class TigaseClient extends TestCase {
	
	private Jaxmpp jaxmpp;
	private String from = "zhangsan@lenovo";
	private String password="123456"; 
	private String to = "lisi@lenovo";
	private String serviceName = "192.168.1.101";
	
	public void setUp(){
		// 本地环境，连接本地库
		jaxmpp = new Jaxmpp();
		
		ConnectionConfiguration connectionConfiguration = jaxmpp.getConnectionConfiguration();
		connectionConfiguration.setConnectionType(ConnectionType.socket);
		connectionConfiguration.setUserJID(from);
		connectionConfiguration.setUserPassword(password);
		connectionConfiguration.setServer(serviceName);
		connectionConfiguration.setPort(5222);
	}
	
	public void tearDown(){
		if(null != jaxmpp) {
			try {
				jaxmpp.disconnect();
			} catch (JaxmppException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void testLogin() throws InterruptedException {
		try {
			jaxmpp.login(true);
			System.out.println("login ok!");
			
			Message message = Message.createMessage();
			message.setBody("李四，你好");
			message.setTo(JID.jidInstance(to));
			// message.setFrom(JID.jidInstance(from));
			jaxmpp.send(message);
			
			// 检测好友上线
//			jaxmpp.addListener(Jaxmpp.Connected, new Listener<JaxmppEvent>() {
//				@Override
//				public void handleEvent(JaxmppEvent be) throws JaxmppException {
//					System.out.println("---" + be.getSessionObject().getUserBareJid().getLocalpart() +"登录成功---");
//				}
//			});
//			
//			jaxmpp.addListener(Jaxmpp.Disconnected,  new Listener<JaxmppEvent>() {
//				@Override
//				public void handleEvent(JaxmppEvent be) throws JaxmppException {
//					System.out.println("---" + be.getSessionObject().getUserBareJid().getLocalpart() +"退出---");
//				}
//			});
//			
//			jaxmpp.sendMessage(toJID, subject, message);
		} catch (Exception e) {
			Assert.assertFalse(true);
		} 	
		System.out.println("end!");
	}
}
